package net.searchdata.ref;

import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import org.junit.Test;

public class Entry28 {
	class Stack<E> {
		private List<E> list = new ArrayList<E>();

		public void push(E e) {
			list.add(e);
		}

		public E pop() {
			return list.remove(list.size() - 1);
		}

		public void pushAll(Collection<? extends E> collection) {
			list.addAll(collection);
		}

		public void popAll(Collection<? super E> dst) {
			while (!isEmpty()) {
				dst.add(pop());
			}
		}

		public boolean isEmpty() {
			return list.isEmpty();
		}
	}

	@Test
	public void stackSimpleTest() {
		Stack<Integer> intStack = new Stack<Integer>();

		intStack.push(1);
		intStack.push(2);
		intStack.push(3);

		assertThat(intStack.pop(), is(3));
		assertThat(intStack.pop(), is(2));
		assertThat(intStack.pop(), is(1));
	}

	@Test
	public void testPushAll() {
		Stack<Number> intStack = new Stack<Number>();
		intStack.pushAll(Arrays.asList(1, 2, 3));

		assertThat(intStack.pop(), is((Number) Integer.valueOf(3)));
		assertThat(intStack.pop(), is((Number) Integer.valueOf(2)));
		assertThat(intStack.pop(), is((Number) Integer.valueOf(1)));
	}

	@Test
	public void testPopAll() {
		List<Number> numberList = new ArrayList<Number>();

		Stack<Integer> intStack = new Stack<Integer>();
		intStack.pushAll(Arrays.asList(1, 2, 3));

		intStack.popAll(numberList);
	}
}
